<template>
   <div>
    <el-dialog
      :title="info.isAdd ? '商品添加' : '商品修改'"
      :visible.sync="info.isshow"
      @close="close"
      @opened="opened"
    >
      <el-form :model="user">
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="change_first_cate">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" label-width="120px">
          <el-input v-model="user.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格" label-width="120px">
          <el-input v-model="user.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格" label-width="120px">
          <el-input v-model="user.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片" label-width="120px">
          <upload-img @sendFile="user.img = $event" ref="upload"></upload-img>
        </el-form-item>
        <div>user:{{ user }}</div>
        <div>attr_arr:{{ attr_arr }}</div>
        <el-form-item label="商品规格" label-width="120px">
          <el-select v-model="user.specsid" @change="change_specs_id">
            <el-option
              v-for="item in specsList"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性" label-width="120px">
          <el-select v-model="user.specsattr" multiple>
            <el-option
              v-for="item in attr_arr"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品" label-width="120px">
          <el-radio v-model="user.isnew" :label="1">是</el-radio>
          <el-radio v-model="user.isnew" :label="2">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖" label-width="120px">
          <el-radio v-model="user.ishot" :label="1">是</el-radio>
          <el-radio v-model="user.ishot" :label="2">否</el-radio>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item label="描述" label-width="120px">
          <div id="editor" v-if="info.isshow"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqcatelist,
  reqgoodsadd,
  reqgoodsedit,
  reqgoodsinfo,
} from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";
import E from "wangeditor";
export default {
  props: ['info'],
  mixins:[formClose],
  data() {
    return {
       user:{
         first_cateid:'',
         second_cateid:'',
         goodsname:'',
         price:'',
         market_price:'',
         img:null,
          description:'',
         specsid:'',
         specsattr:'',
         isnew:2,
         ishot:2,
         status:1,
       },
       //二级分类的列表
       second_cate_list:[],
       //规格属性的列表
       attr_arr:[],
    };
  },
  computed: {
    ...mapGetters({
      cateList:'cate/arr',
      specsList:'specs/arr'
    }),
  },
  mounted() { this.reqCateArr();
     //请求规格
     this.reqSpecsList(true)},
  methods: {
    ...mapActions({
     reqCateArr:'cate/reqArr',
     //取出请求商品规格的方法
     reqSpecsList:'specs/reqList',
     //商品请求
      reqArr: "goods/reqArr",
  
    }),
    //弹框打开 完成，我们要创建编辑器
    opened(){
      this.editor = new E('#editor');
      this.editor.create();
      this.editor.txt.html(this.user.description)
    },
    //选了一级分类
     change_first_cate(){
        //清空二级分类选中的值
    this.user.second_cateid = '';
    this.get_second_cate_list();
  },
  get_second_cate_list(){
    reqcatelist({pid:this.user.first_cateid}).then((res)=>{
      if(res.data.code==200){
        this.second_cate_list = res.data.list
      }
    })
  },
  //修改了规格
  change_specs_id(){
    //选中的规格属性清空
    this.user.specsattr = [];
    //展示的规格属性列表
    this.get_specs_attrs();
  },
  //获取规格属性的列表
  get_specs_attrs(){
    let obj = this.specsList.find((item)=>item.id == this.user.specsid)
    this.attr_arr = obj ? obj.attrs :[]
  },
  //清空user
  empty() {
      this.user = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: null,
        description: "",
        specsid: "",
        specsattr: "",
        isnew: 2,
        ishot: 2,
        status: 1,
      };
       //二级分类的列表
      this.second_cate_list = [];
      //规格属性的列表
      this.attr_arr = [];
      //清除图片
      this.$refs.upload.setImgUrl("");
    },
  //点了添加
  add(){
    //处理attrs
    reqgoodsadd({
      ...this.user,
      specsattr:this.user.specsattr.join(','),
      description:this.editor.txt.html(),
    }).then((res)=>{
      if(res.data.code ==200){
        //弹成功
        successAlert(res.data.msg)
        //弹框内容清空
        this.empty();
        //弹框消失
        this.info.isshow=false;
        //重新请求列表
        this.reqArr()
      }

    })
  },
  //获取一条数据
  getOne(id){
    reqgoodsinfo({id:id}).then((res)=>{
      if(res.data.code===200){
        this.user = res.data.list;
        this.user.id = id;
        this.get_second_cate_list();
        //给图片赋值
        this.$refs.upload.setImgUrl(this.$pre + this.user.img );
        //处理规格属性 
        this.user.specsattr = this.user.specsattr.split(',')
        //展示的规格属性列表
        this.get_specs_attrs();
        if(this.editor){
          this.editor.txt.html(this.user.description)
        }
      }
    })

  },
  //修改
  update(){
    reqgoodsedit({
      ...this.user,
      specsattr:this.user.specsattr.join(','),
       description:this.editor.txt.html(),

    }).then((res)=>{
      if(res.data.code==200){
        successAlert(res.data.msg)
        this.empty();
        this.info.isshow = false;
        this.reqArr()
      }
    })
  }

  },
 

};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>